//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.Research.ScientificWorkflow;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Container Activity for while sequence", "While Sequence")]
    [Description("Container for holding child activities for a while loop.")]
    [WorkflowCategory("Control Flow")]
    [HideActivity(true)]
    public partial class WhileBranch : System.Workflow.Activities.SequenceActivity
	{
		public WhileBranch()
		{
		}

        public static System.Workflow.ComponentModel.DependencyProperty ConditionProperty =
            System.Workflow.ComponentModel.DependencyProperty.Register("Condition", typeof(bool), 
            typeof(WhileBranch));

        [RequiredInputParam]
        [Name("Condtion")]
        [Description("If true the child activities are exectued for one iternation else they are skipped.")]
        public bool Condition
        {
            get { return (bool)GetValue(WhileBranch.ConditionProperty); }
            set { SetValue(WhileBranch.ConditionProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            WhileActivity act = this.Parent.Parent as WhileActivity;
            if (null != act)
            {
                act.Condition = Condition;
            }

            if (Condition == true)
                return base.Execute(executionContext);
            else
            {
                return ActivityExecutionStatus.Closed;
            }
        }
	}
}
